//
//  ZTTabBarController.m
//  ZTuo_QuantivitiTrasaction
//
//  Created by chu on 2018/11/8.
//  Copyright © 2018 chu. All rights reserved.
//

#import "ZTTabBarController.h"

#import "CYHomeViewController.h"
#import "CYProjectViewController.h"
#import "CYVideoViewController.h"
#import "CYMineViewController.h"
#import "CDTabBar.h"

@interface ZTTabBarController ()<UITabBarControllerDelegate, UITabBarDelegate>

@end

@implementation ZTTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addAllChildViewController];
    
    [self InitMiddleView];
    
    UITabBar *tabBar = self.tabBar;
    tabBar.barTintColor = kWhiteColor;
    tabBar.translucent = NO;
    tabBar.barStyle = UIBarStyleBlack;
    
    self.delegate = self;
}

- (void)InitMiddleView
{
    CDTabBar *tabBar = [[CDTabBar alloc] init];
    [self setValue:tabBar forKey:@"tabBar"];
    [tabBar setDidMiddBtn:^{
        [self childViewControllers][0].tabBarController.selectedIndex=4;
    }];
}

- (void)addAllChildViewController{
    
    // 1.初始化子控制器
    CYHomeViewController *home = [[CYHomeViewController alloc] init];
    [self addChildVc:home title:@"首页" navTitle:@"创业一点通" image:@"首页"selectedImage:@"首页sel"];
    
    CYProjectViewController *item = [[CYProjectViewController alloc] init];
    [self addChildVc:item title:@"找项目" navTitle:@"找项目" image:@"项目" selectedImage:@"项目sel"];
    
    //    HZReleaseViewController *release = [[HZReleaseViewController alloc] init];
    //    [self addChildVc:release title:@"发布" navTitle:@"发布" image:@"tabbar_release" selectedImage:@"tabbar_release"];
    
    CYVideoViewController *video = [[CYVideoViewController alloc] init];
    [self addChildVc:video title:@"看视频" navTitle:@"看视频" image:@"视频" selectedImage:@"视频sel"];
    
    CYMineViewController *profile = [[CYMineViewController alloc] init];
    [self addChildVc:profile title:@"我的" navTitle:@"我的" image:@"我的" selectedImage:@"我的sel"];
    
    //    HZTabbar *tabBar = [[HZTabbar alloc] init];
    //    tabBar.delegate = self;
    //    [self setValue:tabBar forKeyPath:@"tabBar"];
    
}

- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title navTitle:(NSString *)navTitle image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置子控制器的文字
    childVc.tabBarItem.title = title;
    childVc.navigationItem.title = navTitle;
    
    // 设置子控制器的图片
    childVc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

//    NSString *name = selectedImage; //图片的名字
//    NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:@"gif"];
//    NSData *imageData = [NSData dataWithContentsOfFile:filePath];
//    childVc.tabBarItem.selectedImage= [[UIImage sd_animatedGIFWithData:imageData] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    // 设置文字的样式
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = RGB(103, 103, 103);
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = RGB(244, 69, 73);
    selectTextAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    
    // 先给外面传进来的小控制器 包装 一个导航控制器
    ZTNavigationController *nav = [[ZTNavigationController alloc] initWithRootViewController:childVc];
    // 添加为子控制器
    [self addChildViewController:nav];
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    NSMutableArray *tabbarArr = [NSMutableArray arrayWithCapacity:0];
    for (UIView *tabbarBtn in self.tabBar.subviews) {
        if ([tabbarBtn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            UIImageView *imageView = [tabbarBtn valueForKey:@"info"];
            [tabbarArr addObject:imageView];
        }
    }
    if (self.selectedIndex == 0) {
        [self createGIFWithImageView:tabbarArr[self.selectedIndex] withGifName:@"选中-首页"];
    }else if (self.selectedIndex == 1){
        [self createGIFWithImageView:tabbarArr[self.selectedIndex] withGifName:@"选中-项目"];
    }else if (self.selectedIndex == 2){
        [self createGIFWithImageView:tabbarArr[self.selectedIndex] withGifName:@"选中-视频"];
    }else{
        [self createGIFWithImageView:tabbarArr[self.selectedIndex] withGifName:@"选中-我的"];
    }
    
}

- (void)createGIFWithImageView:(UIImageView *)imageView withGifName:(NSString *)gifName{
    
    //1.找到gif文件路径
    
    NSString *dataPath = [[NSBundle mainBundle]pathForResource:gifName ofType:@"gif"];
    
    //2.获取gif文件数据
    
    CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:dataPath], NULL);
    
    //3.获取gif文件中图片的个数
    
    size_t count = CGImageSourceGetCount(source);
    
    //4.定义一个变量记录gif播放一轮的时间
    
    float allTime = 0;
    
    //5.定义一个可变数组存放所有图片
    
    NSMutableArray *imageArray = [[NSMutableArray alloc] init];
    
    //6.定义一个可变数组存放每一帧播放的时间
    
    NSMutableArray *timeArray = [[NSMutableArray alloc] init];
    
    //7.每张图片的宽度
    
    NSMutableArray *widthArray = [[NSMutableArray alloc] init];
    
    //8.每张图片的高度
    
    NSMutableArray *heightArray = [[NSMutableArray alloc] init];
    
    
    
    //遍历gif
    
    for (size_t i=0; i<count; i++) {
        
        CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
        
        [imageArray addObject:(__bridge UIImage *)(image)];
        
        CGImageRelease(image);
        
        
        
        //获取图片信息
        
        NSDictionary *info = (__bridge NSDictionary *)CGImageSourceCopyPropertiesAtIndex(source, i, NULL);
        
        NSLog(@"info---%@",info);
        
        //获取宽度
        
        CGFloat width = [[info objectForKey:(__bridge NSString *)kCGImagePropertyPixelWidth] floatValue];
        
        //获取高度
        
        CGFloat height = [[info objectForKey:(__bridge NSString *)kCGImagePropertyPixelHeight] floatValue];
        
        
        
        //
        
        [widthArray addObject:[NSNumber numberWithFloat:width]];
        
        [heightArray addObject:[NSNumber numberWithFloat:height]];
        
        
        
        //统计时间
        
        NSDictionary *timeDic = [info objectForKey:(__bridge NSString *)kCGImagePropertyGIFDictionary];
        
        CGFloat time = [[timeDic objectForKey:(__bridge NSString *)kCGImagePropertyGIFDelayTime] floatValue];
        
        [timeArray addObject:[NSNumber numberWithFloat:time]];
        
        allTime += time;
    }
    
    //添加帧动画
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
//    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"contents"];

    NSMutableArray *times = [[NSMutableArray alloc] init];
    
    float currentTime = 0;
    
    //设置每一帧的时间占比
    
    for (int i=0; i<imageArray.count; i++) {
        
        [times addObject:[NSNumber numberWithFloat:currentTime/allTime]];
        
        currentTime +=[timeArray[i] floatValue];
        
    }
    [animation setKeyTimes:times];
    
    [animation setValues:imageArray];
    
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    
    //设置循环
    
    animation.repeatCount = 0;
    
    //设置播放总时长
    
    animation.duration = allTime;

    //Layer层添加
    
    [[imageView layer] addAnimation:animation forKey:@"gifAnimation"];
//    [imageView startAnimating];
}

@end
